package com.wyq.jpa.controller;

import com.wyq.jpa.dao.UserDao;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import com.baidu.bae.api.util.BaeEnv;

@RequestMapping(value="admin")
@Controller
public class AdminController {
	@Autowired
	private UserDao userDao;
	@RequestMapping(value="login",method=RequestMethod.GET)
	public String login(){
		return "admin/login";
	}

	@RequestMapping(value="login",method=RequestMethod.POST)
	public ModelAndView login2(HttpServletRequest request, HttpServletResponse response){
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		UsernamePasswordToken token = new UsernamePasswordToken(username,
				password);
		ModelAndView mavLogin = new ModelAndView("/admin/login");
		ModelAndView mavMain = new ModelAndView("redirect:main");
		Subject subject = SecurityUtils.getSubject();
		if (subject.isAuthenticated()) {
        	return mavMain;
        }else{
			try {
				subject.login(token);
				token.clear();
				return mavMain;
			} catch (UnknownAccountException ex) {
				ex.printStackTrace();
				mavLogin.addObject("error", "帐号或者密码错误！");
				return mavLogin;
			} catch (IncorrectCredentialsException ex) {
				ex.printStackTrace();
				mavLogin.addObject("error", "认证失败！");
				return mavLogin;
			}
			catch (Exception ex) {
				ex.printStackTrace();
				mavLogin.addObject("error","登录失败 - 不明原因!");
				return mavLogin;
			}
        }
	}
	@RequestMapping(value = "/logout")
    public String logout() {  
        Subject currentUser = SecurityUtils.getSubject();  
        currentUser.logout();  
        return "admin/login";
    }
	@RequestMapping(value="/",method=RequestMethod.GET)
	public String login1(){
		return "admin/login";
	}
	@RequestMapping(value="/main",method=RequestMethod.GET)
	public String main(){
		return "admin/index";
	}
	@RequestMapping(value="/config",method=RequestMethod.GET)
	public String config(){
		return "admin/config/config";
	}
    @RequestMapping(value="/ajaxTimeout",method=RequestMethod.GET)
    public String timeout(){
        return "admin/ajaxTimeout";
    }
    @RequestMapping(value="/blogConfig",method=RequestMethod.GET)
    @RequiresPermissions("user:edit")
    public String blogConfig(){
        return "admin/blogConfig";
    }
	
	/*@RequestMapping(value="/baeconfig",method=RequestMethod.GET)
	public ModelAndView config1(){
		String host = BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_ADDR_SQL_IP);
	 	String port = BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_ADDR_SQL_PORT);
	 	String username = BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_AK);
	 	String password = BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_SK);
	 	ModelAndView mav=new ModelAndView("admin/config/baeconfig");
	 	mav.addObject("host", host);
	 	mav.addObject("port", port);
	 	mav.addObject("username", username);
	 	mav.addObject("password", password);
		return mav;
	}*/
	
}
